package ullio.video
{
	import flash.events.MouseEvent;
	
	import mx.controls.HSlider;
	import mx.controls.Label;
	import mx.controls.ToolTip;
	import mx.controls.VRule;
	import mx.events.SliderEvent;
	
	import ullio.events.AppEvent;
	
 	[Event( name="scrubberMouseDown", type="AppEvent" )]
 	[Event( name="scrubberClick", type="AppEvent" )]

	public class ScrubberSlider extends HSlider
	{
		private var _totalTime:Number;
		public function set totalTime(value:Number):void{
			_totalTime = value;
			maximum = value;	
		}
		public function get totalTime():Number{
			return _totalTime;
		}
				
		private var vrule:VRule;
		private var scrubTip:ToolTip;
		private var allowPlayheadChange:Boolean;
		
		public function ScrubberSlider()
		{
			super();
			useHandCursor = true;
			buttonMode = true;
			addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
			addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);	
			addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);	
			addEventListener(SliderEvent.CHANGE, onSliderChange);
			snapInterval = 0.4;
			minimum = 0;			
			allowTrackClick = true;
			liveDragging = false;
			alpha = .6;												
		}
		override protected function createChildren():void
		{
			super.createChildren();			
			scrubTip = new ToolTip();
			scrubTip.visible = false;
			vrule = new VRule();
			vrule.visible = false;
			addChild(vrule);
			addChild(scrubTip);
		}
		override protected function commitProperties():void
		{
			super.commitProperties();
			scrubTip.styleName = "scrubberToolTip";
		}
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
    	{
    		super.updateDisplayList(unscaledWidth, unscaledHeight);
			vrule.width = 2;
			vrule.height = height / 2;			
			scrubTip.y = -25;
			scrubTip.width = 65;
			scrubTip.height = 25;		
    	}	
		
		// put some super awesome code here to create time tip		
		private function onMouseMove(event:MouseEvent):void{
			//trace("total time: " + totalTime);			
			vrule.x = event.localX;
			scrubTip.x = event.localX - (scrubTip.width/2);
			scrubTip.text = event.localX.toString();			
			vrule.visible = scrubTip.visible = true;			
		}		
		private function onMouseOut(event:MouseEvent):void{			
			vrule.visible = scrubTip.visible = false;
		}
		private function onMouseDown(event:MouseEvent):void{			
			allowPlayheadChange = true;
			dispatchEvent(new AppEvent("scrubberMouseDown"));
		}
		private function onSliderChange(event:SliderEvent):void{
			if (allowPlayheadChange){
				dispatchEvent(new AppEvent("scrubberClick",{time:event.currentTarget.value}));
				allowPlayheadChange = false;
			}
		}		
	}
}